import Vue from 'vue'
import entryFactory from 'bpmn-js-properties-panel/lib/factory/EntryFactory'
const cmdHelper = require('bpmn-js-properties-panel/lib/helper/CmdHelper')
const getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject

const prase = function (data) {
  if (data) {
    return `${data.value}-${data.name}`
  } else {
    return ''
  }
}
export default function (group, element, translate) {
  for (let i = 0; i < group.entries.length; i++) {
    switch (group.entries[i].id) {
      case 'assignee':
        group.entries[i] = entryFactory.textField({
          id: 'assignee',
          label: translate('Assignee'),
          modelProperty: 'assignee',
          get: function (element, node) {
            var bo = getBusinessObject(element)
            var assignee = bo.get('camunda:assignee')
            Vue.prototype.bo = bo
            Vue.prototype.element = element
            return {
              assignee: assignee || prase(Vue.ls.get(`assignee:${document.querySelector('#camunda-id').value}`)) || ''
            }
          },

          set: function (element, values) {
            var bo = getBusinessObject(element)
            console.log(element, values)
            return cmdHelper.updateBusinessObject(element, bo, {
              'camunda:assignee': values.assignee || prase(Vue.ls.get(`assignee:${document.querySelector('#camunda-id').value}`)) || undefined
            })
          }
        })
        break
      case 'candidateUsers':
        group.entries[i] = entryFactory.textField({
          id: 'candidateUsers',
          label: translate('Candidate Users'),
          modelProperty: 'candidateUsers',
          get: function (element, node) {
            var bo = getBusinessObject(element)
            var candidateUsers = bo.get('camunda:candidateUsers')

            return {
              candidateUsers: candidateUsers || prase(Vue.ls.get(`candidateUsers:${document.querySelector('#camunda-id').value}`)) || ''
            }
          },

          set: function (element, values) {
            var bo = getBusinessObject(element)
            return cmdHelper.updateBusinessObject(element, bo, {
              'camunda:candidateUsers': values.candidateUsers || prase(Vue.ls.get(`candidateUsers:${document.querySelector('#camunda-id').value}`)) || undefined
            })
          }
        })
        break
      case 'candidateGroups':
        group.entries[i] = entryFactory.textField({
          id: 'candidateGroups',
          label: translate('Candidate Groups'),
          modelProperty: 'candidateGroups',
          get: function (element, node) {
            var bo = getBusinessObject(element)
            var candidateGroups = bo.get('camunda:candidateGroups')

            return {
              candidateGroups: candidateGroups || prase(Vue.ls.get(`candidateGroups:${document.querySelector('#camunda-id').value}`)) || ''
            }
          },

          set: function (element, values) {
            var bo = getBusinessObject(element)
            return cmdHelper.updateBusinessObject(element, bo, {
              'camunda:candidateGroups': values.candidateGroups || prase(Vue.ls.get(`candidateGroups:${document.querySelector('#camunda-id').value}`)) || undefined
            })
          }
        })
        break
    }
  }
}
